/**
 * 
 */
package com.hw.hcit.feature;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;
import android.os.SystemProperties;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


/**
 * @author gaoshengpan
 *
 */
public class FlashlightTestItem extends JTestItem {
   private Button click ;
   private static final byte[] LIGHTE_ON = { '1' };
   private static final byte[] LIGHTE_OFF = { '0' };
   private int[] status = {0,0};
   private boolean on = false;
   private boolean tested = false;
   private boolean powerClicked = false;
   private String TAG = "FlashlightTestItem";
   private Button mButton1,mButton2;
   /**
    * @param cb
    */
   public FlashlightTestItem(JCtrlCallback cb) {
      super(cb);
      // TODO Auto-generated constructor stub
   }

   /**
    * 
    */
   public FlashlightTestItem() {
      // TODO Auto-generated constructor stub
   }
   
   

   @Override
   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      if(click !=null ){
    	  Log.d(TAG, "------>notNull");
      }
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View rootView = inflater.inflate(R.layout.flashlight_test, container, false);
      click = (Button) rootView.findViewById(R.id.button1);
      mButton1 = (Button) rootView.findViewById(R.id.button2);
	  mButton2 = (Button) rootView.findViewById(R.id.button3);
	  mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(10, 'P');
					} else {
						rewriteOneByteOfNv2499(41, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(10, 'F');
					} else {
						rewriteOneByteOfNv2499(41, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
      click.setOnClickListener(new OnClickListener() {
  		@Override
  		public void onClick(View arg0) {
  			// TODO Auto-generated method stub
  			Log.d(TAG, "------click");

  			if (!on) {
  	            click.setBackgroundColor(Color.BLUE);
  	            setLEDStatus(true);
  	            on = true ;
  	            tested = true;
  	            status[0] = 1;
  	         } else {
   	            click.setBackgroundColor(Color.RED);
  	            setLEDStatus(false);
  	            on = false ;
  	            status[1] = 1;
  	         }
  		}
  	});
       return rootView;
   }

   @Override
   public void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();
   }

   @Override
   public void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      tested = false;
	  powerClicked = false;
      on = false;
      click.setBackgroundColor(Color.RED);
      setLEDStatus(false);
      Log.d(TAG, "readbattery--->"+readFile("/sys/class/power_supply/battery/BatteryAverageCurrent"));
      Log.d(TAG, "readflash--->"+readFile("/sys/class/leds/flashlight/brightness"));
   }
   
   @Override
   public void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      on = false;
      click.setBackgroundColor(Color.RED);
      setLEDStatus(false);
   }
   private void setLEDStatus(boolean status) {
      FileOutputStream red;
     /* if(status){
    	  SystemProperties.set("ctl.start", "set_flash_on");
    	  Log.d(TAG, " --------->on");
      }else{
    	  SystemProperties.set("ctl.start", "set_flash_off");
    	  Log.d(TAG, " --------->on");
      }*/
      try {
         byte[] ledData = LIGHTE_OFF;
         ledData = status ? LIGHTE_ON : LIGHTE_OFF;
         red = new FileOutputStream(
               "/sys/class/leds/flashlight/brightness");
         red.write(ledData);
         red.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   private  String readFile(String filePath){   
		String res="";   
		try{   
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
			String str=null;
			while((str=br.readLine())!=null){
				res+=str;
			}  
			if(br !=null){
				br.close();
			}
       }   
		catch(Exception e){   
        e.printStackTrace();   
       }   
       return res;   
	}  
   
   @Override
   public boolean dispatchKeyEvent(KeyEvent event) {
       // TODO Auto-generated method stub
       return true;
   }
  /* @Override
   public boolean dispatchKeyEvent(KeyEvent event) {
       // TODO Auto-generated method stub
       if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
    	   dissmissPowerClicked();
           Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
           if (powerClicked) {
               Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_VOLUME_DOWN");
               testFinish(false, 1, false);
               return true;
           }
           if (tested) {
               Log.d(TAG, "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
               testFinish(true, 1, true);
           } else {
               Log.d(TAG, " --------->showTestUncomplete");
               showTestUncomplete();
           }
       } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
    	   dissmissPowerClicked();
           testFinish(true, -1, true);
       } else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
           Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_POWER");
           powerClicked = true;
           showPowerClicked();
       }
       return true;
   }*/

   
}
